package com.lhb.ojjudgeservice.judge.judgestrategy;

import cn.hutool.json.JSONUtil;
import com.lhb.ojmodel.model.dto.question.JudgeConfig;
import com.lhb.ojmodel.model.dto.questionsubmit.JudgeInfo;
import com.lhb.ojmodel.model.enums.JudgeInfoMessageEnum;


/*
* 编程语言为java的判题策略
* */
public class JavaJudgeStrategyImpl implements JudgeStrategy{
    @Override
    public JudgeInfo judgeResult(StrategyContext strategyContext) {
        JudgeInfo judgeInfoResponse = new JudgeInfo();
        if(strategyContext.getJudgeInfo() != null){
            judgeInfoResponse.setMemory(strategyContext.getJudgeInfo().getMemory());
            judgeInfoResponse.setTime(strategyContext.getJudgeInfo().getTime());
        }
        JudgeConfig judgeConfig = JSONUtil.toBean(strategyContext.getQuestion().getJudgeConfig(),JudgeConfig.class);
        //先判断沙箱执行的结果输出数量是否和预期输出数量相等
        if(strategyContext.getJudgeOutputList().size() > strategyContext.getOutputList().size()){
            judgeInfoResponse.setMessage(JudgeInfoMessageEnum.OUTPUT_LIMIT_EXCEEDED.getText());
            return judgeInfoResponse;
        }
        //判断输出的结果与预期的结果是否一致
        for (int i = 0; i < strategyContext.getOutputList().size(); i++) {
            if(!strategyContext.getOutputList().get(i).equals(strategyContext.getJudgeOutputList().get(i))){
                judgeInfoResponse.setMessage(JudgeInfoMessageEnum.WRONG_ERROR.getText());
                return judgeInfoResponse;
            }
        }
        //内存溢出
        if (strategyContext.getJudgeInfo().getMemory() > judgeConfig.getMemoryLimit()){
            judgeInfoResponse.setMessage(JudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED.getText());
            return judgeInfoResponse;
        }
        //超时
        long JAVA_TIME = 1000L;
        if((strategyContext.getJudgeInfo().getTime() - JAVA_TIME) > judgeConfig.getTimeLimit()){
            judgeInfoResponse.setMessage(JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED.getText());
            return judgeInfoResponse;
        }
        judgeInfoResponse.setMessage(JudgeInfoMessageEnum.ACCEPT.getText());
        return judgeInfoResponse;
    }
}
